package com.jx.mst.mst.controller;

import cn.hutool.core.bean.BeanUtil;
import com.jx.mst.mst.entity.MstInfo;
import com.jx.mst.mst.entity.dtos.MstInfoByIdDtos;
import com.jx.mst.mst.service.MstDetailService;
import com.jx.mst.mst.service.MstInfoService;
import com.jx.mst.mst.service.MstSelService;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("mstInfoDownLoad")
public class MstFileDownController {
    @Resource
    private MstInfoService mstInfoService;
    @Resource
    private MstDetailService mstDetailService;
    @Resource
    private MstSelService mstSelService;

    @GetMapping("/downs")
    public void xzs(HttpServletResponse response) throws IOException {
        response.setHeader("11111", "1111111" );
        response.setHeader("22222222", "22222222" );
    }

    @GetMapping("/downtest")
    public void downtest(HttpServletResponse response) throws IOException {
        response.setHeader("11111", "1111111" );
        response.setHeader("22222222", "22222222" );
    }

    @GetMapping("/down")
    public void xz(HttpServletResponse response) throws IOException {

        List<MstInfo> list = mstInfoService.query().list();
        List<MstInfoByIdDtos> collect = list.stream().map(item -> {
            MstInfoByIdDtos mstInfoByIdDtos = BeanUtil.copyProperties(item, MstInfoByIdDtos.class);
            mstInfoByIdDtos.setMstDetail(mstDetailService.getById(mstInfoByIdDtos.getId()));
            mstInfoByIdDtos.setMstSel(mstSelService.getById(mstInfoByIdDtos.getMstSelId()));
            return mstInfoByIdDtos;
        }).collect(Collectors.toList());
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("面试题");
        sheet.setColumnWidth(0, 5 * 256);  //第二个参数为列的宽度，一个标准字母的宽度为256
        sheet.setColumnWidth(1, 10 * 256);
        sheet.setColumnWidth(2, 10 * 256);
        sheet.setColumnWidth(3, 10 * 256);
        sheet.setColumnWidth(4, 100 * 256);

        String[] titles = new String[]{"序号", "面试题题目", "面试题描述", "面试题分类","面试题内容"};
        Row titleRow = sheet.createRow(0);
        Cell cell = null;
        for (int i = 0; i < titles.length; i++) {
            cell = titleRow.createCell(i);
            cell.setCellValue(titles[i]);
        }

        int rowIndex = 1;
        Row row = null;
        for (MstInfoByIdDtos mstInfo : collect) {
            row = sheet.createRow(rowIndex);

            cell = row.createCell(0);
            cell.setCellValue(rowIndex);

            cell = row.createCell(1);
            cell.setCellValue(mstInfo.getMstInfoTitle());

            cell = row.createCell(2);
            cell.setCellValue(mstInfo.getMstInfoDes());

            cell = row.createCell(3);
            if(mstInfo.getMstSel()!=null){
                cell.setCellValue(mstInfo.getMstSel().getMstSelName());
            }


            cell = row.createCell(4);
            if(mstInfo.getMstDetail()!=null){
                cell.setCellValue(mstInfo.getMstDetail().getMstDetailInfo());
            }


            rowIndex++;
        }


        String filename = "员工数据.xlsx";
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename,"UTF-8"));
//        response.setHeader("content-disposition", filename);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");


//        ServletOutputStream outputStream = response.getOutputStream();
//        workbook.write(outputStream);
//        outputStream.close();
//        workbook.close();

        workbook.write(response.getOutputStream());
        workbook.close();
    }
}
